//
//  YSRootViewController.m
//  YiShi
//
//  Created by myMac on 15/7/3.
//  Copyright (c) 2015年 myMac. All rights reserved.
//

#import "YSRootViewController.h"
#import "YSTabButton.h"
#import "YSTabBar.h"
#import "YSHomeViewController.h"
#import "YSSearchViewController.h"
#import "YSTradeViewController.h"
#import "YSSetupViewController.h"

@interface YSRootViewController ()<YSTabBarDelegate>
@property(nonatomic,strong)UIViewController *currentViewController;
@property(nonatomic,strong)UIView *containerView;
@property(nonatomic,strong)YSTabBar *tabBar;
@property(nonatomic,copy)NSArray *contentControllers;
@end

@implementation YSRootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    // Do any additional setup after loading the view.
    [self _initTabbar];
    [self _initControllers];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

#pragma mark - init methods
- (void)_initTabbar {
    YSTabButton *homeTabBarButton = [YSTabButton buttonWithType:UIButtonTypeCustom];
    homeTabBarButton.tabTitle = @"首页";
    homeTabBarButton.tabImageName = @"tabbar_home";
    homeTabBarButton.tabHighlightedImageName = @"tabbar_home_selected";
    
    YSTabButton *searchTabBarButton = [YSTabButton buttonWithType:UIButtonTypeCustom];
    searchTabBarButton.tabTitle = @"搜索";
    searchTabBarButton.tabImageName = @"tabbar_search";
    searchTabBarButton.tabHighlightedImageName = @"tabbar_search_selected";
    
    YSTabButton *dealTabBarButton = [YSTabButton buttonWithType:UIButtonTypeCustom];
    dealTabBarButton.tabTitle = @"交艺";
    dealTabBarButton.tabImageName = @"tabbar_deal";
    dealTabBarButton.tabHighlightedImageName = @"tabbar_deal_selected";
    
    YSTabButton *setupTabBarButton = [YSTabButton buttonWithType:UIButtonTypeCustom];
    setupTabBarButton.tabTitle = @"设置";
    setupTabBarButton.tabImageName = @"tabbar_setup";
    setupTabBarButton.tabHighlightedImageName = @"tabbar_setup_selected";
    
    YSTabBar *tabBar = [[YSTabBar alloc] init];
    tabBar.delegate = self;
    tabBar.backgroundColor = [UIColor whiteColor];
    tabBar.frame = CGRectMake(0, self.view.bounds.size.height - 49, self.view.bounds.size.width, 49);
    [self.view addSubview:tabBar];
    tabBar.items = @[homeTabBarButton,searchTabBarButton,dealTabBarButton,setupTabBarButton];
    self.tabBar = tabBar;
}

- (void)_initControllers {
    YSHomeViewController *homeViewContoller = [[YSHomeViewController alloc] init];
    YSSearchViewController *searchViewContoller = [[YSSearchViewController alloc] init];
    YSTradeViewController *tradeViewContoller = [[YSTradeViewController alloc] init];
    YSSetupViewController *personViewContoller = [[YSSetupViewController alloc] init];
    self.contentControllers = @[homeViewContoller,searchViewContoller,tradeViewContoller,personViewContoller];
    
    for (UIViewController *controller in self.contentControllers) {
        [self addChildViewController:controller];
    }
    UIViewController *firstControl = [self.contentControllers objectAtIndex:0];
    [_containerView addSubview:firstControl.view];
    self.currentViewController = firstControl;
    firstControl.view.frame = self.containerView.bounds;
}
#pragma mark - tabbar delegate
- (void)YSTabBar:(YSTabBar *)tabBar didSelectedIndex:(NSInteger)selectedIndex {
    self.tabBar.userInteractionEnabled = NO;
    UIViewController *currentController = self.currentViewController;
    UIViewController *nextControl = [self.contentControllers objectAtIndex:selectedIndex];
    __weak YSRootViewController *weakSelf = self;
    [self transitionFromViewController:currentController
                      toViewController:nextControl
                              duration:0.5f
                               options:UIViewAnimationOptionTransitionCrossDissolve
                            animations:^{
                                
                            }
                            completion:^(BOOL finished) {
                                nextControl.view.frame = weakSelf.containerView.bounds;
                                weakSelf.currentViewController = nextControl;
                                weakSelf.tabBar.userInteractionEnabled = YES;
                                [nextControl willMoveToParentViewController:weakSelf];
                            }];
}
@end
